package com.example.administrator.liveapp.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.example.administrator.liveapp.ApplicationUtils;
import com.example.administrator.liveapp.R;
import com.example.administrator.liveapp.Xutils3.MyCallBack;
import com.example.administrator.liveapp.Xutils3.XUtils;
import com.example.administrator.liveapp.activity.CancellationPage;
import com.example.administrator.liveapp.activity.MyReservationPage;
import com.example.administrator.liveapp.bean.LiveOnlineBean;
import com.example.administrator.liveapp.utils.AppUrl;
import com.example.administrator.liveapp.utils.TimeChangeUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/6/21 0021.
 */

public class ReservationTab_2RecyclerViewAdapter extends RecyclerView.Adapter<ReservationTab_2RecyclerViewAdapter.ViewHolder> implements View.OnClickListener{
    public List<LiveOnlineBean.VideoBean> video;
    public Context context;
    MyReservationPage myReservationPage;
    public ReservationTab_2RecyclerViewAdapter(List<LiveOnlineBean.VideoBean> video, MyReservationPage myReservationPage) {
        this.video = video;
        this.context = myReservationPage;
    }

    @Override
    public ReservationTab_2RecyclerViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.reservation_tab2_recy_item,null);
        ViewHolder viewHolder = new ViewHolder(view);
        context = parent.getContext();
        //将创建的View注册点击事件
        view.setOnClickListener(this);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ReservationTab_2RecyclerViewAdapter.ViewHolder holder, final int position) {
        holder.activity_author.setText(video.get(position).getVideoauthor());
        holder.activity_title.setText(video.get(position).getVideotitle());
        if(video.get(position).getVideostatime()!=null){
            holder.activity_endtime.setText(TimeChangeUtil.getStrTime(video.get(position).getVideostatime()));
        }
        holder.activity_place.setText(video.get(position).getVideoplace());
        if(video.get(position).getVideoimgurl()!=null){
            String Url = video.get(position).getVideoimgurl();
            String ImageUrl = Url.substring(1, Url.indexOf("|"));
            Glide.with(context)
                    .load(AppUrl.URL+ImageUrl)
                    .into(holder.reservation_image);
        }
        holder.cancel_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd-HH:mm:ss");
                String str = sdf.format(new java.util.Date());
                long times = Long.parseLong(TimeChangeUtil.getTimeStamp(str));//系统当前时间戳
                long stoptime = (times - Long.parseLong(video.get(position).getVideostatime()))*1000;
                if(stoptime<=0){
                    if(video.get(position).getNeedPay().equals("0")){
                        String url = AppUrl.URLS + AppUrl.videoorderUrl;
                        Map<String,String> map = new HashMap<>();
                        map.put("consumerId", ApplicationUtils.getInstance().loginBean.getConsumerid());
                        map.put("videoId",video.get(position).getVideoid());
                        map.put("statics","2");
                        map.put("status","1");
                        XUtils.Get(url,map,new MyCallBack<String>(){
                            @Override
                            public void onSuccess(String result) {
                                super.onSuccess(result);
                                JSONObject jsonObject = null;
                                try {
                                    jsonObject = new JSONObject(result);
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                                String code = jsonObject.optString("result");
                                if(code.equals("success")){
                                    Toast.makeText(context,"已取消预约",Toast.LENGTH_SHORT).show();
                                    video.remove(position);
                                    notifyDataSetChanged();
                                }else{
                                    Toast.makeText(context,"取消预约失败",Toast.LENGTH_SHORT).show();
                                }
                            }

                            @Override
                            public void onError(Throwable ex, boolean isOnCallback) {
                                super.onError(ex, isOnCallback);
                            }
                        });
                    }else if(video.get(position).getNeedPay().equals("1")){
                        String url = AppUrl.URLS + AppUrl.refunds;
                        Map<String,String> map = new HashMap<>();
                        map.put("consumerId", ApplicationUtils.getInstance().loginBean.getConsumerid());
                        map.put("videoId",video.get(position).getVideoid());
                        XUtils.Get(url,map,new MyCallBack<String>(){
                            @Override
                            public void onSuccess(String result) {
                                super.onSuccess(result);
                                JSONObject jsonObject = null;
                                try {
                                    jsonObject = new JSONObject(result);
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                                String code = jsonObject.optString("result");
                                if(code.equals("success")){
                                    Toast.makeText(context,"已退款",Toast.LENGTH_SHORT).show();
                                    video.remove(position);
                                    notifyDataSetChanged();
                                }else{
                                    Toast.makeText(context,"退款失败",Toast.LENGTH_SHORT).show();
                                }
                            }

                            @Override
                            public void onError(Throwable ex, boolean isOnCallback) {
                                super.onError(ex, isOnCallback);
                            }
                        });
                    }

                }else{
                    Toast.makeText(context, "直播已开始，无法取消预约", Toast.LENGTH_SHORT).show();
                }

            }
        });
        //将position保存在itemView的Tag中，以便点击时进行获取
        holder.itemView.setTag(position);
    }

    @Override
    public int getItemCount() {
        return video.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        public ImageView reservation_image;
        public TextView activity_author;
        public TextView activity_title;
        public TextView activity_endtime;
        public TextView activity_place;
        public Button cancel_button;
        public ViewHolder(View itemView) {
            super(itemView);
            reservation_image = (ImageView) itemView.findViewById(R.id.reservation_image);
            activity_author = (TextView) itemView.findViewById(R.id.activity_author);
            activity_title = (TextView) itemView.findViewById(R.id.activity_title);
            activity_endtime = (TextView) itemView.findViewById(R.id.activity_endtime);
            activity_place = (TextView) itemView.findViewById(R.id.activity_place);
            cancel_button = (Button) itemView.findViewById(R.id.cancel_button);
        }
    }

    @Override
    public void onClick(View v) {
        if (mOnItemClickListener != null) {
            //注意这里使用getTag方法获取position
            mOnItemClickListener.onItemClick(v,(int)v.getTag());
        }
    }
    private ReservationTab_2RecyclerViewAdapter.OnItemClickListener mOnItemClickListener = null;

    //define interface
    public static interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    public void setOnItemClickListener(ReservationTab_2RecyclerViewAdapter.OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

}
